blob: 63191ca5c1f2c3944b549662603922d277566e21 [file] [log] [blame]
[email protected]69085bc52012-10-15 16:41:581# 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]94be7f702014-02-03 19:06:457See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for more details about the presubmit API built into depot_tools.
[email protected]69085bc52012-10-15 16:41:589"""
10
[email protected]1d993172012-10-18 18:15:0411import re
12
Fabrice de Gansbeaed632021-06-23 17:21:0413USE_PYTHON3 = True
tfarina26ef0f02015-02-02 14:50:2514CC_SOURCE_FILES=(r'^cc[\\/].*\.(cc|h)$',)
[email protected]1d993172012-10-18 18:15:0415
[email protected]cf824592013-04-09 05:46:0016def CheckChangeLintsClean(input_api, output_api):
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:1117 allowlist = CC_SOURCE_FILES
18 denylist = None
19 source_filter = lambda x: input_api.FilterSourceFile(x, allowlist, denylist)
[email protected]cf824592013-04-09 05:46:0020
tfarina2d649012015-02-26 12:58:2621 return input_api.canned_checks.CheckChangeLintsClean(
22 input_api, output_api, source_filter, lint_filters=[], verbose_level=1)
[email protected]cf824592013-04-09 05:46:0023
Josip Sokcevic8b6cc432020-08-05 17:45:3324def CheckAsserts(input_api, output_api, allowlist=CC_SOURCE_FILES,
25 denylist=None):
Sadrul Habib Chowdhuryc1f75b22020-07-25 13:27:2526 denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:1127 source_file_filter = lambda x: input_api.FilterSourceFile(x, allowlist,
28 denylist)
[email protected]1d993172012-10-18 18:15:0429
30 assert_files = []
[email protected]1d993172012-10-18 18:15:0431
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]f4a4b0e2012-10-22 22:01:3735 if re.search(r"\bASSERT\(", contents):
[email protected]1d993172012-10-18 18:15:0436 assert_files.append(f.LocalPath())
[email protected]1d993172012-10-18 18:15:0437
38 if assert_files:
39 return [output_api.PresubmitError(
40 'These files use ASSERT instead of using DCHECK:',
41 items=assert_files)]
[email protected]1d993172012-10-18 18:15:0442 return []
43
[email protected]81d205442013-07-29 21:42:0344def CheckStdAbs(input_api, output_api,
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:1145 allowlist=CC_SOURCE_FILES, denylist=None):
Sadrul Habib Chowdhuryc1f75b22020-07-25 13:27:2546 denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
[email protected]81d205442013-07-29 21:42:0347 source_file_filter = lambda x: input_api.FilterSourceFile(x,
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:1148 allowlist,
49 denylist)
[email protected]81d205442013-07-29 21:42:0350
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]dbddc7462013-09-06 06:33:3161
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]81d205442013-07-29 21:42:0372 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]7add2e0b2013-04-23 05:16:4290def CheckPassByValue(input_api,
91 output_api,
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:1192 allowlist=CC_SOURCE_FILES,
93 denylist=None):
Sadrul Habib Chowdhuryc1f75b22020-07-25 13:27:2594 denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
[email protected]7add2e0b2013-04-23 05:16:4295 source_file_filter = lambda x: input_api.FilterSourceFile(x,
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:1196 allowlist,
97 denylist)
[email protected]7add2e0b2013-04-23 05:16:4298
99 local_errors = []
100
kylechare68cc4a2017-04-05 18:01:05101 # Well-defined simple classes the same size as a primitive type.
[email protected]7add2e0b2013-04-23 05:16:42102 pass_by_value_types = ['base::Time',
103 'base::TimeTicks',
[email protected]7add2e0b2013-04-23 05:16:42104 ]
105
106 for f in input_api.AffectedSourceFiles(source_file_filter):
107 contents = input_api.ReadFile(f, 'rb')
Fabrice de Gansbeaed632021-06-23 17:21:04108 sep = '|'
[email protected]7add2e0b2013-04-23 05:16:42109 match = re.search(
Fabrice de Gansbeaed632021-06-23 17:21:04110 r'\bconst +' + '(?P<type>(%s))&' % sep.join(pass_by_value_types),
[email protected]7add2e0b2013-04-23 05:16:42111 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]d936f902013-01-06 05:08:07117
[email protected]6dc0b6f2013-06-26 20:21:59118def 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]932aff42013-06-27 12:59:27124 if ('FIX'+'ME') in contents:
[email protected]6dc0b6f2013-06-26 20:21:59125 errors.append(f.LocalPath())
126
127 if errors:
128 return [output_api.PresubmitError(
kylechare68cc4a2017-04-05 18:01:05129 'All TODO comments should be of the form TODO(name/bug). ' +
weiliangc941dbec2014-08-28 18:56:16130 'Use TODO instead of FIX' + 'ME',
[email protected]6dc0b6f2013-06-26 20:21:59131 items=errors)]
132 return []
133
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:11134def CheckDoubleAngles(input_api, output_api, allowlist=CC_SOURCE_FILES,
135 denylist=None):
danakj6496cba2014-10-16 01:31:08136 errors = []
137
138 source_file_filter = lambda x: input_api.FilterSourceFile(x,
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:11139 allowlist,
140 denylist)
danakj6496cba2014-10-16 01:31:08141 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]e51444a2013-12-10 23:05:01150def 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]3f52c8932014-06-13 08:46:38154def 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 Chowdhury8eb63d12020-07-02 22:27:11167def FindNamespaceInBlock(pos, namespace, contents, allowlist=[]):
[email protected]e51444a2013-12-10 23:05:01168 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 Chowdhury8eb63d12020-07-02 22:27:11198 in_allowlist = False
199 for w in allowlist:
[email protected]e51444a2013-12-10 23:05:01200 if re.match(w, contents[next:]):
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:11201 in_allowlist = True
[email protected]e51444a2013-12-10 23:05:01202 break
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:11203 if not in_allowlist:
[email protected]e51444a2013-12-10 23:05:01204 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.
210def 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 Chowdhury8eb63d12020-07-02 22:27:11218 allowlist = []
219 if FindNamespaceInBlock(match.end(), 'cc', contents, allowlist=allowlist):
[email protected]e51444a2013-12-10 23:05:01220 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]d2f1d582014-05-01 08:43:53228def CheckForUseOfWrongClock(input_api,
229 output_api,
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:11230 allowlist=CC_SOURCE_FILES,
231 denylist=None):
[email protected]d2f1d582014-05-01 08:43:53232 """Make sure new lines of code don't use a clock susceptible to skew."""
Sadrul Habib Chowdhuryc1f75b22020-07-25 13:27:25233 denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
[email protected]d2f1d582014-05-01 08:43:53234 source_file_filter = lambda x: input_api.FilterSourceFile(x,
Sadrul Habib Chowdhury8eb63d12020-07-02 22:27:11235 allowlist,
236 denylist)
[email protected]d2f1d582014-05-01 08:43:53237 # 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]6dc0b6f2013-06-26 20:21:59280
[email protected]1d993172012-10-18 18:15:04281def CheckChangeOnUpload(input_api, output_api):
282 results = []
283 results += CheckAsserts(input_api, output_api)
[email protected]81d205442013-07-29 21:42:03284 results += CheckStdAbs(input_api, output_api)
[email protected]7add2e0b2013-04-23 05:16:42285 results += CheckPassByValue(input_api, output_api)
[email protected]cf824592013-04-09 05:46:00286 results += CheckChangeLintsClean(input_api, output_api)
[email protected]6dc0b6f2013-06-26 20:21:59287 results += CheckTodos(input_api, output_api)
danakj6496cba2014-10-16 01:31:08288 results += CheckDoubleAngles(input_api, output_api)
[email protected]e51444a2013-12-10 23:05:01289 results += CheckNamespace(input_api, output_api)
[email protected]d2f1d582014-05-01 08:43:53290 results += CheckForUseOfWrongClock(input_api, output_api)
[email protected]3f52c8932014-06-13 08:46:38291 results += FindUselessIfdefs(input_api, output_api)
[email protected]1d993172012-10-18 18:15:04292 return results