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