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 15from __future__ import print_function 16import itertools 17import subprocess 18import time 19 20 21def RunAndReportIfLong(func, *args, **kargs): 22 start = time.time() 23 results = func(*args, **kargs) 24 end = time.time() 25 limit = 0.5 # seconds 26 name = func.__name__ 27 runtime = end - start 28 if runtime > limit: 29 print("{} took >{:.2}s ({:.2}s)".format(name, limit, runtime)) 30 return results 31 32 33def CheckChange(input, output): 34 # There apparently is no way to wrap strings in blueprints, so ignore long 35 # lines in them. 36 def long_line_sources(x): 37 return input.FilterSourceFile( 38 x, 39 files_to_check='.*', 40 files_to_skip=[ 41 'Android[.]bp', 42 '.*[.]json$', 43 '.*[.]sql$', 44 '.*[.]out$', 45 'test/trace_processor/.*/index$', 46 '(.*/)?BUILD$', 47 'WORKSPACE', 48 '.*/Makefile$', 49 '/perfetto_build_flags.h$', 50 "infra/luci/.*", 51 ]) 52 53 results = [] 54 results += RunAndReportIfLong(input.canned_checks.CheckDoNotSubmit, input, 55 output) 56 results += RunAndReportIfLong(input.canned_checks.CheckChangeHasNoTabs, input, 57 output) 58 results += RunAndReportIfLong( 59 input.canned_checks.CheckLongLines, 60 input, 61 output, 62 80, 63 source_file_filter=long_line_sources) 64 results += RunAndReportIfLong( 65 input.canned_checks.CheckPatchFormatted, input, output, check_js=True) 66 results += RunAndReportIfLong(input.canned_checks.CheckGNFormatted, input, 67 output) 68 results += RunAndReportIfLong(CheckIncludeGuards, input, output) 69 results += RunAndReportIfLong(CheckIncludeViolations, input, output) 70 results += RunAndReportIfLong(CheckProtoComments, input, output) 71 results += RunAndReportIfLong(CheckBuild, input, output) 72 results += RunAndReportIfLong(CheckAndroidBlueprint, input, output) 73 results += RunAndReportIfLong(CheckBinaryDescriptors, input, output) 74 results += RunAndReportIfLong(CheckMergedTraceConfigProto, input, output) 75 results += RunAndReportIfLong(CheckProtoEventList, input, output) 76 results += RunAndReportIfLong(CheckBannedCpp, input, output) 77 results += RunAndReportIfLong(CheckSqlMetrics, input, output) 78 results += RunAndReportIfLong(CheckTestData, input, output) 79 return results 80 81 82def CheckChangeOnUpload(input_api, output_api): 83 return CheckChange(input_api, output_api) 84 85 86def CheckChangeOnCommit(input_api, output_api): 87 return CheckChange(input_api, output_api) 88 89 90def CheckBuild(input_api, output_api): 91 tool = 'tools/gen_bazel' 92 93 # If no GN files were modified, bail out. 94 def build_file_filter(x): 95 return input_api.FilterSourceFile( 96 x, files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', 'BUILD\.extras', tool)) 97 98 if not input_api.AffectedSourceFiles(build_file_filter): 99 return [] 100 if subprocess.call([tool, '--check-only']): 101 return [ 102 output_api.PresubmitError('Bazel BUILD(s) are out of date. Run ' + 103 tool + ' to update them.') 104 ] 105 return [] 106 107 108def CheckAndroidBlueprint(input_api, output_api): 109 tool = 'tools/gen_android_bp' 110 111 # If no GN files were modified, bail out. 112 def build_file_filter(x): 113 return input_api.FilterSourceFile( 114 x, files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', tool)) 115 116 if not input_api.AffectedSourceFiles(build_file_filter): 117 return [] 118 if subprocess.call([tool, '--check-only']): 119 return [ 120 output_api.PresubmitError('Android build files are out of date. ' + 121 'Run ' + tool + ' to update them.') 122 ] 123 return [] 124 125 126def CheckIncludeGuards(input_api, output_api): 127 tool = 'tools/fix_include_guards' 128 129 def file_filter(x): 130 return input_api.FilterSourceFile( 131 x, files_to_check=['.*[.]cc$', '.*[.]h$', tool]) 132 133 if not input_api.AffectedSourceFiles(file_filter): 134 return [] 135 if subprocess.call([tool, '--check-only']): 136 return [ 137 output_api.PresubmitError('Please run ' + tool + 138 ' to fix include guards.') 139 ] 140 return [] 141 142 143def CheckBannedCpp(input_api, output_api): 144 bad_cpp = [ 145 (r'\bstd::stoi\b', 146 'std::stoi throws exceptions prefer base::StringToInt32()'), 147 (r'\bstd::stol\b', 148 'std::stoull throws exceptions prefer base::StringToInt32()'), 149 (r'\bstd::stoul\b', 150 'std::stoull throws exceptions prefer base::StringToUint32()'), 151 (r'\bstd::stoll\b', 152 'std::stoull throws exceptions prefer base::StringToInt64()'), 153 (r'\bstd::stoull\b', 154 'std::stoull throws exceptions prefer base::StringToUint64()'), 155 (r'\bstd::stof\b', 156 'std::stof throws exceptions prefer base::StringToDouble()'), 157 (r'\bstd::stod\b', 158 'std::stod throws exceptions prefer base::StringToDouble()'), 159 (r'\bstd::stold\b', 160 'std::stold throws exceptions prefer base::StringToDouble()'), 161 (r'\bstrncpy\b', 162 'strncpy does not null-terminate if src > dst. Use base::StringCopy'), 163 (r'[(=]\s*snprintf\(', 164 'snprintf can return > dst_size. Use base::SprintfTrunc'), 165 (r'//.*\bDNS\b', 166 '// DNS (Do Not Ship) found. Did you mean to remove some testing code?'), 167 (r'\bPERFETTO_EINTR\(close\(', 168 'close(2) must not be retried on EINTR on Linux and other OSes ' 169 'that we run on, as the fd will be closed.'), 170 (r'^#include <inttypes.h>', 'Use <cinttypes> rather than <inttypes.h>. ' + 171 'See https://github.com/google/perfetto/issues/146'), 172 ] 173 174 def file_filter(x): 175 return input_api.FilterSourceFile(x, files_to_check=[r'.*\.h$', r'.*\.cc$']) 176 177 errors = [] 178 for f in input_api.AffectedSourceFiles(file_filter): 179 for line_number, line in f.ChangedContents(): 180 if input_api.re.search(r'^\s*//', line): 181 continue # Skip comments 182 for regex, message in bad_cpp: 183 if input_api.re.search(regex, line): 184 errors.append( 185 output_api.PresubmitError('Banned pattern:\n {}:{} {}'.format( 186 f.LocalPath(), line_number, message))) 187 return errors 188 189 190def CheckIncludeViolations(input_api, output_api): 191 tool = 'tools/check_include_violations' 192 193 def file_filter(x): 194 return input_api.FilterSourceFile( 195 x, files_to_check=['include/.*[.]h$', tool]) 196 197 if not input_api.AffectedSourceFiles(file_filter): 198 return [] 199 if subprocess.call([tool]): 200 return [output_api.PresubmitError(tool + ' failed.')] 201 return [] 202 203 204def CheckBinaryDescriptors(input_api, output_api): 205 tool = 'tools/gen_binary_descriptors' 206 207 def file_filter(x): 208 return input_api.FilterSourceFile( 209 x, files_to_check=['protos/perfetto/.*[.]proto$', '.*[.]h', tool]) 210 211 if not input_api.AffectedSourceFiles(file_filter): 212 return [] 213 if subprocess.call([tool, '--check-only']): 214 return [ 215 output_api.PresubmitError('Please run ' + tool + 216 ' to update binary descriptors.') 217 ] 218 return [] 219 220 221def CheckMergedTraceConfigProto(input_api, output_api): 222 tool = 'tools/gen_merged_protos' 223 224 def build_file_filter(x): 225 return input_api.FilterSourceFile( 226 x, files_to_check=['protos/perfetto/.*[.]proto$', tool]) 227 228 if not input_api.AffectedSourceFiles(build_file_filter): 229 return [] 230 if subprocess.call([tool, '--check-only']): 231 return [ 232 output_api.PresubmitError( 233 'perfetto_config.proto or perfetto_trace.proto is out of ' + 234 'date. Please run ' + tool + ' to update it.') 235 ] 236 return [] 237 238 239# Prevent removing or changing lines in event_list. 240def CheckProtoEventList(input_api, output_api): 241 for f in input_api.AffectedFiles(): 242 if f.LocalPath() != 'tools/ftrace_proto_gen/event_list': 243 continue 244 if any((not new_line.startswith('removed')) and new_line != old_line 245 for old_line, new_line in zip(f.OldContents(), f.NewContents())): 246 return [ 247 output_api.PresubmitError( 248 'event_list only has two supported changes: ' 249 'appending a new line, and replacing a line with removed.') 250 ] 251 return [] 252 253 254def CheckProtoComments(input_api, output_api): 255 tool = 'tools/check_proto_comments' 256 257 def file_filter(x): 258 return input_api.FilterSourceFile( 259 x, files_to_check=['protos/perfetto/.*[.]proto$', tool]) 260 261 if not input_api.AffectedSourceFiles(file_filter): 262 return [] 263 if subprocess.call([tool]): 264 return [output_api.PresubmitError(tool + ' failed')] 265 return [] 266 267 268def CheckSqlMetrics(input_api, output_api): 269 tool = 'tools/check_sql_metrics.py' 270 271 def file_filter(x): 272 return input_api.FilterSourceFile( 273 x, files_to_check=['src/trace_processor/metrics/.*[.]sql$', tool]) 274 275 if not input_api.AffectedSourceFiles(file_filter): 276 return [] 277 if subprocess.call([tool]): 278 return [output_api.PresubmitError(tool + ' failed')] 279 return [] 280 281 282def CheckTestData(input_api, output_api): 283 tool = 'tools/test_data' 284 if subprocess.call([tool, 'status', '--quiet']): 285 return [ 286 output_api.PresubmitError( 287 '//test/data is out of sync. Run ' + tool + ' status for more. \n' + 288 'If you rebaselined UI tests or added a new test trace, run: \n' + 289 'tools/test_data upload') 290 ] 291 return [] 292