1 # Copyright 2012 The Chromium Authors
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Top-level presubmit script for cc.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into depot_tools.
14 CC_SOURCE_FILES=(r'^cc[\\/].*\.(cc|h)$',)
16 def CheckChangeLintsClean(input_api, output_api):
17 allowlist = CC_SOURCE_FILES
19 source_filter = lambda x: input_api.FilterSourceFile(x, allowlist, denylist)
21 return input_api.canned_checks.CheckChangeLintsClean(
22 input_api, output_api, source_filter, lint_filters=[], verbose_level=1)
24 def CheckAsserts(input_api, output_api, allowlist=CC_SOURCE_FILES,
26 denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
27 source_file_filter = lambda x: input_api.FilterSourceFile(x, allowlist,
32 for f in input_api.AffectedSourceFiles(source_file_filter):
33 contents = input_api.ReadFile(f, 'rb')
34 # WebKit ASSERT() is not allowed.
35 if re.search(r"\bASSERT\(", contents):
36 assert_files.append(f.LocalPath())
39 return [output_api.PresubmitError(
40 'These files use ASSERT instead of using DCHECK:',
44 def CheckStdAbs(input_api, output_api,
45 allowlist=CC_SOURCE_FILES, denylist=None):
46 denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
47 source_file_filter = lambda x: input_api.FilterSourceFile(x,
51 using_std_abs_files = []
53 missing_std_prefix_files = []
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());
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)"
69 expression = re.compile("(%s|%s)%s" %
70 (abs_without_prefix, fabs_without_prefix, no_nolint))
71 if expression.search(contents):
72 missing_std_prefix_files.append(f.LocalPath())
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))
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))
90 def CheckPassByValue(input_api,
92 allowlist=CC_SOURCE_FILES,
94 denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
95 source_file_filter = lambda x: input_api.FilterSourceFile(x,
101 # Well-defined simple classes the same size as a primitive type.
102 pass_by_value_types = ['base::Time',
106 for f in input_api.AffectedSourceFiles(source_file_filter):
107 contents = input_api.ReadFile(f, 'rb')
110 r'\bconst +' + '(?P<type>(%s))&' % sep.join(pass_by_value_types),
113 local_errors.append(output_api.PresubmitError(
114 '%s passes %s by const ref instead of by value.' %
115 (f.LocalPath(), match.group('type'))))
118 def CheckTodos(input_api, output_api):
121 source_file_filter = lambda x: x
122 for f in input_api.AffectedSourceFiles(source_file_filter):
123 contents = input_api.ReadFile(f, 'rb')
124 if ('FIX'+'ME') in contents:
125 errors.append(f.LocalPath())
128 return [output_api.PresubmitError(
129 'All TODO comments should be of the form TODO(name/bug). ' +
130 'Use TODO instead of FIX' + 'ME',
134 def CheckDoubleAngles(input_api, output_api, allowlist=CC_SOURCE_FILES,
138 source_file_filter = lambda x: input_api.FilterSourceFile(x,
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())
147 return [output_api.PresubmitError('Use >> instead of > >:', items=errors)]
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
154 def FindUselessIfdefs(input_api, output_api):
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())
162 return [output_api.PresubmitError(
163 'Don\'t use #if '+'0; just delete the code.',
167 def FindNamespaceInBlock(pos, namespace, contents, allowlist=[]):
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)
181 return False # The namespace is not used at all.
183 open_brace = len(contents)
185 close_brace = len(contents)
187 quote = len(contents)
189 next = min(open_brace, min(close_brace, min(quote, name)))
191 if next == open_brace:
193 elif next == close_brace:
196 quote_count = 0 if quote_count else 1
197 elif next == name and not quote_count:
200 if re.match(w, contents[next:]):
208 # Checks for the use of cc:: within the cc namespace, which is usually
210 def CheckNamespace(input_api, output_api):
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)
219 if FindNamespaceInBlock(match.end(), 'cc', contents, allowlist=allowlist):
220 errors.append(f.LocalPath())
223 return [output_api.PresubmitError(
224 'Do not use cc:: inside of the cc namespace.',
228 def CheckForUseOfWrongClock(input_api,
230 allowlist=CC_SOURCE_FILES,
232 """Make sure new lines of code don't use a clock susceptible to skew."""
233 denylist = tuple(denylist or input_api.DEFAULT_FILES_TO_SKIP)
234 source_file_filter = lambda x: input_api.FilterSourceFile(x,
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|$)'
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)::'
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:
252 # int64 foo_us = foo_s * Time::kMicrosecondsPerSecond;
253 using_base_time_decl_pattern = r'^\s*using\s+(::)?base::Time\s*;'
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+'
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')')
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):
270 ' %s:%d\n %s' % (f.LocalPath(), line_number, line.strip()))
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))]
281 def CheckChangeOnUpload(input_api, output_api):
283 results += CheckAsserts(input_api, output_api)
284 results += CheckStdAbs(input_api, output_api)
285 results += CheckPassByValue(input_api, output_api)
286 results += CheckChangeLintsClean(input_api, output_api)
287 results += CheckTodos(input_api, output_api)
288 results += CheckDoubleAngles(input_api, output_api)
289 results += CheckNamespace(input_api, output_api)
290 results += CheckForUseOfWrongClock(input_api, output_api)
291 results += FindUselessIfdefs(input_api, output_api)