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