[email protected] | 69085bc5 | 2012-10-15 16:41:58 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Top-level presubmit script for cc. |
| 6 | |
[email protected] | 94be7f70 | 2014-02-03 19:06:45 | [diff] [blame] | 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 8 | for more details about the presubmit API built into depot_tools. |
[email protected] | 69085bc5 | 2012-10-15 16:41:58 | [diff] [blame] | 9 | """ |
| 10 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 11 | import re |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 12 | import string |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 13 | |
tfarina | 26ef0f0 | 2015-02-02 14:50:25 | [diff] [blame] | 14 | CC_SOURCE_FILES=(r'^cc[\\/].*\.(cc|h)$',) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 15 | |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 16 | def CheckChangeLintsClean(input_api, output_api): |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 17 | source_filter = lambda x: input_api.FilterSourceFile( |
| 18 | x, white_list=CC_SOURCE_FILES, black_list=None) |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 19 | |
tfarina | 2d64901 | 2015-02-26 12:58:26 | [diff] [blame^] | 20 | return input_api.canned_checks.CheckChangeLintsClean( |
| 21 | input_api, output_api, source_filter, lint_filters=[], verbose_level=1) |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 22 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 23 | def CheckAsserts(input_api, output_api, white_list=CC_SOURCE_FILES, black_list=None): |
| 24 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 25 | source_file_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list) |
| 26 | |
| 27 | assert_files = [] |
| 28 | notreached_files = [] |
| 29 | |
| 30 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 31 | contents = input_api.ReadFile(f, 'rb') |
| 32 | # WebKit ASSERT() is not allowed. |
[email protected] | f4a4b0e | 2012-10-22 22:01:37 | [diff] [blame] | 33 | if re.search(r"\bASSERT\(", contents): |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 34 | assert_files.append(f.LocalPath()) |
| 35 | # WebKit ASSERT_NOT_REACHED() is not allowed. |
| 36 | if re.search(r"ASSERT_NOT_REACHED\(", contents): |
| 37 | notreached_files.append(f.LocalPath()) |
| 38 | |
| 39 | if assert_files: |
| 40 | return [output_api.PresubmitError( |
| 41 | 'These files use ASSERT instead of using DCHECK:', |
| 42 | items=assert_files)] |
| 43 | if notreached_files: |
| 44 | return [output_api.PresubmitError( |
| 45 | 'These files use ASSERT_NOT_REACHED instead of using NOTREACHED:', |
| 46 | items=notreached_files)] |
| 47 | return [] |
| 48 | |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 49 | def CheckStdAbs(input_api, output_api, |
| 50 | white_list=CC_SOURCE_FILES, black_list=None): |
| 51 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 52 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
| 53 | white_list, |
| 54 | black_list) |
| 55 | |
| 56 | using_std_abs_files = [] |
| 57 | found_fabs_files = [] |
| 58 | missing_std_prefix_files = [] |
| 59 | |
| 60 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 61 | contents = input_api.ReadFile(f, 'rb') |
| 62 | if re.search(r"using std::f?abs;", contents): |
| 63 | using_std_abs_files.append(f.LocalPath()) |
| 64 | if re.search(r"\bfabsf?\(", contents): |
| 65 | found_fabs_files.append(f.LocalPath()); |
[email protected] | dbddc746 | 2013-09-06 06:33:31 | [diff] [blame] | 66 | |
| 67 | no_std_prefix = r"(?<!std::)" |
| 68 | # Matches occurrences of abs/absf/fabs/fabsf without a "std::" prefix. |
| 69 | abs_without_prefix = r"%s(\babsf?\()" % no_std_prefix |
| 70 | fabs_without_prefix = r"%s(\bfabsf?\()" % no_std_prefix |
| 71 | # Skips matching any lines that have "// NOLINT". |
| 72 | no_nolint = r"(?![^\n]*//\s+NOLINT)" |
| 73 | |
| 74 | expression = re.compile("(%s|%s)%s" % |
| 75 | (abs_without_prefix, fabs_without_prefix, no_nolint)) |
| 76 | if expression.search(contents): |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 77 | missing_std_prefix_files.append(f.LocalPath()) |
| 78 | |
| 79 | result = [] |
| 80 | if using_std_abs_files: |
| 81 | result.append(output_api.PresubmitError( |
| 82 | 'These files have "using std::abs" which is not permitted.', |
| 83 | items=using_std_abs_files)) |
| 84 | if found_fabs_files: |
| 85 | result.append(output_api.PresubmitError( |
| 86 | 'std::abs() should be used instead of std::fabs() for consistency.', |
| 87 | items=found_fabs_files)) |
| 88 | if missing_std_prefix_files: |
| 89 | result.append(output_api.PresubmitError( |
| 90 | 'These files use abs(), absf(), fabs(), or fabsf() without qualifying ' |
| 91 | 'the std namespace. Please use std::abs() in all places.', |
| 92 | items=missing_std_prefix_files)) |
| 93 | return result |
| 94 | |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 95 | def CheckPassByValue(input_api, |
| 96 | output_api, |
| 97 | white_list=CC_SOURCE_FILES, |
| 98 | black_list=None): |
| 99 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 100 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
| 101 | white_list, |
| 102 | black_list) |
| 103 | |
| 104 | local_errors = [] |
| 105 | |
| 106 | # Well-defined simple classes containing only <= 4 ints, or <= 2 floats. |
| 107 | pass_by_value_types = ['base::Time', |
| 108 | 'base::TimeTicks', |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 109 | ] |
| 110 | |
| 111 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 112 | contents = input_api.ReadFile(f, 'rb') |
| 113 | match = re.search( |
| 114 | r'\bconst +' + '(?P<type>(%s))&' % |
| 115 | string.join(pass_by_value_types, '|'), |
| 116 | contents) |
| 117 | if match: |
| 118 | local_errors.append(output_api.PresubmitError( |
| 119 | '%s passes %s by const ref instead of by value.' % |
| 120 | (f.LocalPath(), match.group('type')))) |
| 121 | return local_errors |
[email protected] | d936f90 | 2013-01-06 05:08:07 | [diff] [blame] | 122 | |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 123 | def CheckTodos(input_api, output_api): |
| 124 | errors = [] |
| 125 | |
| 126 | source_file_filter = lambda x: x |
| 127 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 128 | contents = input_api.ReadFile(f, 'rb') |
[email protected] | 932aff4 | 2013-06-27 12:59:27 | [diff] [blame] | 129 | if ('FIX'+'ME') in contents: |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 130 | errors.append(f.LocalPath()) |
| 131 | |
| 132 | if errors: |
| 133 | return [output_api.PresubmitError( |
weiliangc | 941dbec | 2014-08-28 18:56:16 | [diff] [blame] | 134 | 'All TODO comments should be of the form TODO(name). ' + |
| 135 | 'Use TODO instead of FIX' + 'ME', |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 136 | items=errors)] |
| 137 | return [] |
| 138 | |
danakj | 6496cba | 2014-10-16 01:31:08 | [diff] [blame] | 139 | def CheckDoubleAngles(input_api, output_api, white_list=CC_SOURCE_FILES, |
| 140 | black_list=None): |
| 141 | errors = [] |
| 142 | |
| 143 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
| 144 | white_list, |
| 145 | black_list) |
| 146 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 147 | contents = input_api.ReadFile(f, 'rb') |
| 148 | if ('> >') in contents: |
| 149 | errors.append(f.LocalPath()) |
| 150 | |
| 151 | if errors: |
| 152 | return [output_api.PresubmitError('Use >> instead of > >:', items=errors)] |
| 153 | return [] |
| 154 | |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 155 | def CheckScopedPtr(input_api, output_api, |
| 156 | white_list=CC_SOURCE_FILES, black_list=None): |
| 157 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 158 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
| 159 | white_list, |
| 160 | black_list) |
| 161 | errors = [] |
| 162 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 163 | for line_number, line in f.ChangedContents(): |
| 164 | # Disallow: |
| 165 | # return scoped_ptr<T>(foo); |
| 166 | # bar = scoped_ptr<T>(foo); |
| 167 | # But allow: |
| 168 | # return scoped_ptr<T[]>(foo); |
| 169 | # bar = scoped_ptr<T[]>(foo); |
| 170 | if re.search(r'(=|\breturn)\s*scoped_ptr<.*?(?<!])>\([^)]+\)', line): |
| 171 | errors.append(output_api.PresubmitError( |
| 172 | ('%s:%d uses explicit scoped_ptr constructor. ' + |
| 173 | 'Use make_scoped_ptr() instead.') % (f.LocalPath(), line_number))) |
| 174 | # Disallow: |
danakj | 968153f3 | 2014-10-15 22:52:16 | [diff] [blame] | 175 | # scoped_ptr<T>() |
| 176 | if re.search(r'\bscoped_ptr<.*?>\(\)', line): |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 177 | errors.append(output_api.PresubmitError( |
| 178 | '%s:%d uses scoped_ptr<T>(). Use nullptr instead.' % |
| 179 | (f.LocalPath(), line_number))) |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 180 | return errors |
| 181 | |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 182 | def FindUnquotedQuote(contents, pos): |
| 183 | match = re.search(r"(?<!\\)(?P<quote>\")", contents[pos:]) |
| 184 | return -1 if not match else match.start("quote") + pos |
| 185 | |
[email protected] | 3f52c893 | 2014-06-13 08:46:38 | [diff] [blame] | 186 | def FindUselessIfdefs(input_api, output_api): |
| 187 | errors = [] |
| 188 | source_file_filter = lambda x: x |
| 189 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 190 | contents = input_api.ReadFile(f, 'rb') |
| 191 | if re.search(r'#if\s*0\s', contents): |
| 192 | errors.append(f.LocalPath()) |
| 193 | if errors: |
| 194 | return [output_api.PresubmitError( |
| 195 | 'Don\'t use #if '+'0; just delete the code.', |
| 196 | items=errors)] |
| 197 | return [] |
| 198 | |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 199 | def FindNamespaceInBlock(pos, namespace, contents, whitelist=[]): |
| 200 | open_brace = -1 |
| 201 | close_brace = -1 |
| 202 | quote = -1 |
| 203 | name = -1 |
| 204 | brace_count = 1 |
| 205 | quote_count = 0 |
| 206 | while pos < len(contents) and brace_count > 0: |
| 207 | if open_brace < pos: open_brace = contents.find("{", pos) |
| 208 | if close_brace < pos: close_brace = contents.find("}", pos) |
| 209 | if quote < pos: quote = FindUnquotedQuote(contents, pos) |
| 210 | if name < pos: name = contents.find(("%s::" % namespace), pos) |
| 211 | |
| 212 | if name < 0: |
| 213 | return False # The namespace is not used at all. |
| 214 | if open_brace < 0: |
| 215 | open_brace = len(contents) |
| 216 | if close_brace < 0: |
| 217 | close_brace = len(contents) |
| 218 | if quote < 0: |
| 219 | quote = len(contents) |
| 220 | |
| 221 | next = min(open_brace, min(close_brace, min(quote, name))) |
| 222 | |
| 223 | if next == open_brace: |
| 224 | brace_count += 1 |
| 225 | elif next == close_brace: |
| 226 | brace_count -= 1 |
| 227 | elif next == quote: |
| 228 | quote_count = 0 if quote_count else 1 |
| 229 | elif next == name and not quote_count: |
| 230 | in_whitelist = False |
| 231 | for w in whitelist: |
| 232 | if re.match(w, contents[next:]): |
| 233 | in_whitelist = True |
| 234 | break |
| 235 | if not in_whitelist: |
| 236 | return True |
| 237 | pos = next + 1 |
| 238 | return False |
| 239 | |
| 240 | # Checks for the use of cc:: within the cc namespace, which is usually |
| 241 | # redundant. |
| 242 | def CheckNamespace(input_api, output_api): |
| 243 | errors = [] |
| 244 | |
| 245 | source_file_filter = lambda x: x |
| 246 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 247 | contents = input_api.ReadFile(f, 'rb') |
| 248 | match = re.search(r'namespace\s*cc\s*{', contents) |
| 249 | if match: |
| 250 | whitelist = [ |
| 251 | r"cc::remove_if\b", |
| 252 | ] |
| 253 | if FindNamespaceInBlock(match.end(), 'cc', contents, whitelist=whitelist): |
| 254 | errors.append(f.LocalPath()) |
| 255 | |
| 256 | if errors: |
| 257 | return [output_api.PresubmitError( |
| 258 | 'Do not use cc:: inside of the cc namespace.', |
| 259 | items=errors)] |
| 260 | return [] |
| 261 | |
[email protected] | d2f1d58 | 2014-05-01 08:43:53 | [diff] [blame] | 262 | def CheckForUseOfWrongClock(input_api, |
| 263 | output_api, |
| 264 | white_list=CC_SOURCE_FILES, |
| 265 | black_list=None): |
| 266 | """Make sure new lines of code don't use a clock susceptible to skew.""" |
| 267 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 268 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
| 269 | white_list, |
| 270 | black_list) |
| 271 | # Regular expression that should detect any explicit references to the |
| 272 | # base::Time type (or base::Clock/DefaultClock), whether in using decls, |
| 273 | # typedefs, or to call static methods. |
| 274 | base_time_type_pattern = r'(^|\W)base::(Time|Clock|DefaultClock)(\W|$)' |
| 275 | |
| 276 | # Regular expression that should detect references to the base::Time class |
| 277 | # members, such as a call to base::Time::Now. |
| 278 | base_time_member_pattern = r'(^|\W)(Time|Clock|DefaultClock)::' |
| 279 | |
| 280 | # Regular expression to detect "using base::Time" declarations. We want to |
| 281 | # prevent these from triggerring a warning. For example, it's perfectly |
| 282 | # reasonable for code to be written like this: |
| 283 | # |
| 284 | # using base::Time; |
| 285 | # ... |
| 286 | # int64 foo_us = foo_s * Time::kMicrosecondsPerSecond; |
| 287 | using_base_time_decl_pattern = r'^\s*using\s+(::)?base::Time\s*;' |
| 288 | |
| 289 | # Regular expression to detect references to the kXXX constants in the |
| 290 | # base::Time class. We want to prevent these from triggerring a warning. |
| 291 | base_time_konstant_pattern = r'(^|\W)Time::k\w+' |
| 292 | |
| 293 | problem_re = input_api.re.compile( |
| 294 | r'(' + base_time_type_pattern + r')|(' + base_time_member_pattern + r')') |
| 295 | exception_re = input_api.re.compile( |
| 296 | r'(' + using_base_time_decl_pattern + r')|(' + |
| 297 | base_time_konstant_pattern + r')') |
| 298 | problems = [] |
| 299 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 300 | for line_number, line in f.ChangedContents(): |
| 301 | if problem_re.search(line): |
| 302 | if not exception_re.search(line): |
| 303 | problems.append( |
| 304 | ' %s:%d\n %s' % (f.LocalPath(), line_number, line.strip())) |
| 305 | |
| 306 | if problems: |
| 307 | return [output_api.PresubmitPromptOrNotify( |
| 308 | 'You added one or more references to the base::Time class and/or one\n' |
| 309 | 'of its member functions (or base::Clock/DefaultClock). In cc code,\n' |
| 310 | 'it is most certainly incorrect! Instead use base::TimeTicks.\n\n' |
| 311 | '\n'.join(problems))] |
| 312 | else: |
| 313 | return [] |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 314 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 315 | def CheckChangeOnUpload(input_api, output_api): |
| 316 | results = [] |
| 317 | results += CheckAsserts(input_api, output_api) |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 318 | results += CheckStdAbs(input_api, output_api) |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 319 | results += CheckPassByValue(input_api, output_api) |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 320 | results += CheckChangeLintsClean(input_api, output_api) |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 321 | results += CheckTodos(input_api, output_api) |
danakj | 6496cba | 2014-10-16 01:31:08 | [diff] [blame] | 322 | results += CheckDoubleAngles(input_api, output_api) |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 323 | results += CheckScopedPtr(input_api, output_api) |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 324 | results += CheckNamespace(input_api, output_api) |
[email protected] | d2f1d58 | 2014-05-01 08:43:53 | [diff] [blame] | 325 | results += CheckForUseOfWrongClock(input_api, output_api) |
[email protected] | 3f52c893 | 2014-06-13 08:46:38 | [diff] [blame] | 326 | results += FindUselessIfdefs(input_api, output_api) |
[email protected] | 4f0fd02 | 2014-01-30 08:05:22 | [diff] [blame] | 327 | results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 328 | return results |
| 329 | |
[email protected] | 634c8a6f | 2014-03-11 17:22:59 | [diff] [blame] | 330 | def GetPreferredTryMasters(project, change): |
| 331 | return { |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 332 | 'tryserver.blink': { |
[email protected] | 0094fa1 | 2014-03-13 03:18:28 | [diff] [blame] | 333 | 'linux_blink_rel': set(['defaulttests']), |
[email protected] | ed504094 | 2014-03-18 03:16:29 | [diff] [blame] | 334 | }, |
[email protected] | 634c8a6f | 2014-03-11 17:22:59 | [diff] [blame] | 335 | } |