[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 |
| 12 | |
Fabrice de Gans | beaed63 | 2021-06-23 17:21:04 | [diff] [blame] | 13 | USE_PYTHON3 = True |
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): |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 17 | allowlist = CC_SOURCE_FILES |
| 18 | denylist = None |
| 19 | source_filter = lambda x: input_api.FilterSourceFile(x, allowlist, denylist) |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 20 | |
tfarina | 2d64901 | 2015-02-26 12:58:26 | [diff] [blame] | 21 | return input_api.canned_checks.CheckChangeLintsClean( |
| 22 | input_api, output_api, source_filter, lint_filters=[], verbose_level=1) |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 23 | |
Josip Sokcevic | 8b6cc43 | 2020-08-05 17:45:33 | [diff] [blame] | 24 | def CheckAsserts(input_api, output_api, allowlist=CC_SOURCE_FILES, |
| 25 | denylist=None): |
Sadrul Habib Chowdhury | c1f75b2 | 2020-07-25 13:27:25 | [diff] [blame] | 26 | denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP) |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 27 | source_file_filter = lambda x: input_api.FilterSourceFile(x, allowlist, |
| 28 | denylist) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 29 | |
| 30 | assert_files = [] |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 31 | |
| 32 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 33 | contents = input_api.ReadFile(f, 'rb') |
| 34 | # WebKit ASSERT() is not allowed. |
[email protected] | f4a4b0e | 2012-10-22 22:01:37 | [diff] [blame] | 35 | if re.search(r"\bASSERT\(", contents): |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 36 | assert_files.append(f.LocalPath()) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 37 | |
| 38 | if assert_files: |
| 39 | return [output_api.PresubmitError( |
| 40 | 'These files use ASSERT instead of using DCHECK:', |
| 41 | items=assert_files)] |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 42 | return [] |
| 43 | |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 44 | def CheckStdAbs(input_api, output_api, |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 45 | allowlist=CC_SOURCE_FILES, denylist=None): |
Sadrul Habib Chowdhury | c1f75b2 | 2020-07-25 13:27:25 | [diff] [blame] | 46 | denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP) |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 47 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 48 | allowlist, |
| 49 | denylist) |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 50 | |
| 51 | using_std_abs_files = [] |
| 52 | found_fabs_files = [] |
| 53 | missing_std_prefix_files = [] |
| 54 | |
| 55 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 56 | contents = input_api.ReadFile(f, 'rb') |
| 57 | if re.search(r"using std::f?abs;", contents): |
| 58 | using_std_abs_files.append(f.LocalPath()) |
| 59 | if re.search(r"\bfabsf?\(", contents): |
| 60 | found_fabs_files.append(f.LocalPath()); |
[email protected] | dbddc746 | 2013-09-06 06:33:31 | [diff] [blame] | 61 | |
| 62 | no_std_prefix = r"(?<!std::)" |
| 63 | # Matches occurrences of abs/absf/fabs/fabsf without a "std::" prefix. |
| 64 | abs_without_prefix = r"%s(\babsf?\()" % no_std_prefix |
| 65 | fabs_without_prefix = r"%s(\bfabsf?\()" % no_std_prefix |
| 66 | # Skips matching any lines that have "// NOLINT". |
| 67 | no_nolint = r"(?![^\n]*//\s+NOLINT)" |
| 68 | |
| 69 | expression = re.compile("(%s|%s)%s" % |
| 70 | (abs_without_prefix, fabs_without_prefix, no_nolint)) |
| 71 | if expression.search(contents): |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 72 | missing_std_prefix_files.append(f.LocalPath()) |
| 73 | |
| 74 | result = [] |
| 75 | if using_std_abs_files: |
| 76 | result.append(output_api.PresubmitError( |
| 77 | 'These files have "using std::abs" which is not permitted.', |
| 78 | items=using_std_abs_files)) |
| 79 | if found_fabs_files: |
| 80 | result.append(output_api.PresubmitError( |
| 81 | 'std::abs() should be used instead of std::fabs() for consistency.', |
| 82 | items=found_fabs_files)) |
| 83 | if missing_std_prefix_files: |
| 84 | result.append(output_api.PresubmitError( |
| 85 | 'These files use abs(), absf(), fabs(), or fabsf() without qualifying ' |
| 86 | 'the std namespace. Please use std::abs() in all places.', |
| 87 | items=missing_std_prefix_files)) |
| 88 | return result |
| 89 | |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 90 | def CheckPassByValue(input_api, |
| 91 | output_api, |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 92 | allowlist=CC_SOURCE_FILES, |
| 93 | denylist=None): |
Sadrul Habib Chowdhury | c1f75b2 | 2020-07-25 13:27:25 | [diff] [blame] | 94 | denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP) |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 95 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 96 | allowlist, |
| 97 | denylist) |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 98 | |
| 99 | local_errors = [] |
| 100 | |
kylechar | e68cc4a | 2017-04-05 18:01:05 | [diff] [blame] | 101 | # Well-defined simple classes the same size as a primitive type. |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 102 | pass_by_value_types = ['base::Time', |
| 103 | 'base::TimeTicks', |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 104 | ] |
| 105 | |
| 106 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 107 | contents = input_api.ReadFile(f, 'rb') |
Fabrice de Gans | beaed63 | 2021-06-23 17:21:04 | [diff] [blame] | 108 | sep = '|' |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 109 | match = re.search( |
Fabrice de Gans | beaed63 | 2021-06-23 17:21:04 | [diff] [blame] | 110 | r'\bconst +' + '(?P<type>(%s))&' % sep.join(pass_by_value_types), |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 111 | contents) |
| 112 | if match: |
| 113 | local_errors.append(output_api.PresubmitError( |
| 114 | '%s passes %s by const ref instead of by value.' % |
| 115 | (f.LocalPath(), match.group('type')))) |
| 116 | return local_errors |
[email protected] | d936f90 | 2013-01-06 05:08:07 | [diff] [blame] | 117 | |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 118 | def CheckTodos(input_api, output_api): |
| 119 | errors = [] |
| 120 | |
| 121 | source_file_filter = lambda x: x |
| 122 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 123 | contents = input_api.ReadFile(f, 'rb') |
[email protected] | 932aff4 | 2013-06-27 12:59:27 | [diff] [blame] | 124 | if ('FIX'+'ME') in contents: |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 125 | errors.append(f.LocalPath()) |
| 126 | |
| 127 | if errors: |
| 128 | return [output_api.PresubmitError( |
kylechar | e68cc4a | 2017-04-05 18:01:05 | [diff] [blame] | 129 | 'All TODO comments should be of the form TODO(name/bug). ' + |
weiliangc | 941dbec | 2014-08-28 18:56:16 | [diff] [blame] | 130 | 'Use TODO instead of FIX' + 'ME', |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 131 | items=errors)] |
| 132 | return [] |
| 133 | |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 134 | def CheckDoubleAngles(input_api, output_api, allowlist=CC_SOURCE_FILES, |
| 135 | denylist=None): |
danakj | 6496cba | 2014-10-16 01:31:08 | [diff] [blame] | 136 | errors = [] |
| 137 | |
| 138 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 139 | allowlist, |
| 140 | denylist) |
danakj | 6496cba | 2014-10-16 01:31:08 | [diff] [blame] | 141 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 142 | contents = input_api.ReadFile(f, 'rb') |
| 143 | if ('> >') in contents: |
| 144 | errors.append(f.LocalPath()) |
| 145 | |
| 146 | if errors: |
| 147 | return [output_api.PresubmitError('Use >> instead of > >:', items=errors)] |
| 148 | return [] |
| 149 | |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 150 | def FindUnquotedQuote(contents, pos): |
| 151 | match = re.search(r"(?<!\\)(?P<quote>\")", contents[pos:]) |
| 152 | return -1 if not match else match.start("quote") + pos |
| 153 | |
[email protected] | 3f52c893 | 2014-06-13 08:46:38 | [diff] [blame] | 154 | def FindUselessIfdefs(input_api, output_api): |
| 155 | errors = [] |
| 156 | source_file_filter = lambda x: x |
| 157 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 158 | contents = input_api.ReadFile(f, 'rb') |
| 159 | if re.search(r'#if\s*0\s', contents): |
| 160 | errors.append(f.LocalPath()) |
| 161 | if errors: |
| 162 | return [output_api.PresubmitError( |
| 163 | 'Don\'t use #if '+'0; just delete the code.', |
| 164 | items=errors)] |
| 165 | return [] |
| 166 | |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 167 | def FindNamespaceInBlock(pos, namespace, contents, allowlist=[]): |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 168 | open_brace = -1 |
| 169 | close_brace = -1 |
| 170 | quote = -1 |
| 171 | name = -1 |
| 172 | brace_count = 1 |
| 173 | quote_count = 0 |
| 174 | while pos < len(contents) and brace_count > 0: |
| 175 | if open_brace < pos: open_brace = contents.find("{", pos) |
| 176 | if close_brace < pos: close_brace = contents.find("}", pos) |
| 177 | if quote < pos: quote = FindUnquotedQuote(contents, pos) |
| 178 | if name < pos: name = contents.find(("%s::" % namespace), pos) |
| 179 | |
| 180 | if name < 0: |
| 181 | return False # The namespace is not used at all. |
| 182 | if open_brace < 0: |
| 183 | open_brace = len(contents) |
| 184 | if close_brace < 0: |
| 185 | close_brace = len(contents) |
| 186 | if quote < 0: |
| 187 | quote = len(contents) |
| 188 | |
| 189 | next = min(open_brace, min(close_brace, min(quote, name))) |
| 190 | |
| 191 | if next == open_brace: |
| 192 | brace_count += 1 |
| 193 | elif next == close_brace: |
| 194 | brace_count -= 1 |
| 195 | elif next == quote: |
| 196 | quote_count = 0 if quote_count else 1 |
| 197 | elif next == name and not quote_count: |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 198 | in_allowlist = False |
| 199 | for w in allowlist: |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 200 | if re.match(w, contents[next:]): |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 201 | in_allowlist = True |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 202 | break |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 203 | if not in_allowlist: |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 204 | return True |
| 205 | pos = next + 1 |
| 206 | return False |
| 207 | |
| 208 | # Checks for the use of cc:: within the cc namespace, which is usually |
| 209 | # redundant. |
| 210 | def CheckNamespace(input_api, output_api): |
| 211 | errors = [] |
| 212 | |
| 213 | source_file_filter = lambda x: x |
| 214 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 215 | contents = input_api.ReadFile(f, 'rb') |
| 216 | match = re.search(r'namespace\s*cc\s*{', contents) |
| 217 | if match: |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 218 | allowlist = [] |
| 219 | if FindNamespaceInBlock(match.end(), 'cc', contents, allowlist=allowlist): |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 220 | errors.append(f.LocalPath()) |
| 221 | |
| 222 | if errors: |
| 223 | return [output_api.PresubmitError( |
| 224 | 'Do not use cc:: inside of the cc namespace.', |
| 225 | items=errors)] |
| 226 | return [] |
| 227 | |
[email protected] | d2f1d58 | 2014-05-01 08:43:53 | [diff] [blame] | 228 | def CheckForUseOfWrongClock(input_api, |
| 229 | output_api, |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 230 | allowlist=CC_SOURCE_FILES, |
| 231 | denylist=None): |
[email protected] | d2f1d58 | 2014-05-01 08:43:53 | [diff] [blame] | 232 | """Make sure new lines of code don't use a clock susceptible to skew.""" |
Sadrul Habib Chowdhury | c1f75b2 | 2020-07-25 13:27:25 | [diff] [blame] | 233 | denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP) |
[email protected] | d2f1d58 | 2014-05-01 08:43:53 | [diff] [blame] | 234 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
Sadrul Habib Chowdhury | 8eb63d1 | 2020-07-02 22:27:11 | [diff] [blame] | 235 | allowlist, |
| 236 | denylist) |
[email protected] | d2f1d58 | 2014-05-01 08:43:53 | [diff] [blame] | 237 | # Regular expression that should detect any explicit references to the |
| 238 | # base::Time type (or base::Clock/DefaultClock), whether in using decls, |
| 239 | # typedefs, or to call static methods. |
| 240 | base_time_type_pattern = r'(^|\W)base::(Time|Clock|DefaultClock)(\W|$)' |
| 241 | |
| 242 | # Regular expression that should detect references to the base::Time class |
| 243 | # members, such as a call to base::Time::Now. |
| 244 | base_time_member_pattern = r'(^|\W)(Time|Clock|DefaultClock)::' |
| 245 | |
| 246 | # Regular expression to detect "using base::Time" declarations. We want to |
| 247 | # prevent these from triggerring a warning. For example, it's perfectly |
| 248 | # reasonable for code to be written like this: |
| 249 | # |
| 250 | # using base::Time; |
| 251 | # ... |
| 252 | # int64 foo_us = foo_s * Time::kMicrosecondsPerSecond; |
| 253 | using_base_time_decl_pattern = r'^\s*using\s+(::)?base::Time\s*;' |
| 254 | |
| 255 | # Regular expression to detect references to the kXXX constants in the |
| 256 | # base::Time class. We want to prevent these from triggerring a warning. |
| 257 | base_time_konstant_pattern = r'(^|\W)Time::k\w+' |
| 258 | |
| 259 | problem_re = input_api.re.compile( |
| 260 | r'(' + base_time_type_pattern + r')|(' + base_time_member_pattern + r')') |
| 261 | exception_re = input_api.re.compile( |
| 262 | r'(' + using_base_time_decl_pattern + r')|(' + |
| 263 | base_time_konstant_pattern + r')') |
| 264 | problems = [] |
| 265 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 266 | for line_number, line in f.ChangedContents(): |
| 267 | if problem_re.search(line): |
| 268 | if not exception_re.search(line): |
| 269 | problems.append( |
| 270 | ' %s:%d\n %s' % (f.LocalPath(), line_number, line.strip())) |
| 271 | |
| 272 | if problems: |
| 273 | return [output_api.PresubmitPromptOrNotify( |
| 274 | 'You added one or more references to the base::Time class and/or one\n' |
| 275 | 'of its member functions (or base::Clock/DefaultClock). In cc code,\n' |
| 276 | 'it is most certainly incorrect! Instead use base::TimeTicks.\n\n' |
| 277 | '\n'.join(problems))] |
| 278 | else: |
| 279 | return [] |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 280 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 281 | def CheckChangeOnUpload(input_api, output_api): |
| 282 | results = [] |
| 283 | results += CheckAsserts(input_api, output_api) |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 284 | results += CheckStdAbs(input_api, output_api) |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 285 | results += CheckPassByValue(input_api, output_api) |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 286 | results += CheckChangeLintsClean(input_api, output_api) |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 287 | results += CheckTodos(input_api, output_api) |
danakj | 6496cba | 2014-10-16 01:31:08 | [diff] [blame] | 288 | results += CheckDoubleAngles(input_api, output_api) |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 289 | results += CheckNamespace(input_api, output_api) |
[email protected] | d2f1d58 | 2014-05-01 08:43:53 | [diff] [blame] | 290 | results += CheckForUseOfWrongClock(input_api, output_api) |
[email protected] | 3f52c893 | 2014-06-13 08:46:38 | [diff] [blame] | 291 | results += FindUselessIfdefs(input_api, output_api) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 292 | return results |