MSan: temporarily blacklist an uninit in CLD2.
[chromium-blink-merge.git] / cc / PRESUBMIT.py
blob3397313bd95f109498f038f6ddc9b123bbe66e66
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.
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.
9 """
11 import re
12 import string
14 CC_SOURCE_FILES=(r'^cc/.*\.(cc|h)$',)
16 def CheckChangeLintsClean(input_api, output_api):
17 input_api.cpplint._cpplint_state.ResetErrorCounts() # reset global state
18 source_filter = lambda x: input_api.FilterSourceFile(
19 x, white_list=CC_SOURCE_FILES, black_list=None)
20 files = [f.AbsoluteLocalPath() for f in
21 input_api.AffectedSourceFiles(source_filter)]
22 level = 1 # strict, but just warn
24 for file_name in files:
25 input_api.cpplint.ProcessFile(file_name, level)
27 if not input_api.cpplint._cpplint_state.error_count:
28 return []
30 return [output_api.PresubmitPromptWarning(
31 'Changelist failed cpplint.py check.')]
33 def CheckAsserts(input_api, output_api, white_list=CC_SOURCE_FILES, black_list=None):
34 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
35 source_file_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list)
37 assert_files = []
38 notreached_files = []
40 for f in input_api.AffectedSourceFiles(source_file_filter):
41 contents = input_api.ReadFile(f, 'rb')
42 # WebKit ASSERT() is not allowed.
43 if re.search(r"\bASSERT\(", contents):
44 assert_files.append(f.LocalPath())
45 # WebKit ASSERT_NOT_REACHED() is not allowed.
46 if re.search(r"ASSERT_NOT_REACHED\(", contents):
47 notreached_files.append(f.LocalPath())
49 if assert_files:
50 return [output_api.PresubmitError(
51 'These files use ASSERT instead of using DCHECK:',
52 items=assert_files)]
53 if notreached_files:
54 return [output_api.PresubmitError(
55 'These files use ASSERT_NOT_REACHED instead of using NOTREACHED:',
56 items=notreached_files)]
57 return []
59 def CheckStdAbs(input_api, output_api,
60 white_list=CC_SOURCE_FILES, black_list=None):
61 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
62 source_file_filter = lambda x: input_api.FilterSourceFile(x,
63 white_list,
64 black_list)
66 using_std_abs_files = []
67 found_fabs_files = []
68 missing_std_prefix_files = []
70 for f in input_api.AffectedSourceFiles(source_file_filter):
71 contents = input_api.ReadFile(f, 'rb')
72 if re.search(r"using std::f?abs;", contents):
73 using_std_abs_files.append(f.LocalPath())
74 if re.search(r"\bfabsf?\(", contents):
75 found_fabs_files.append(f.LocalPath());
77 no_std_prefix = r"(?<!std::)"
78 # Matches occurrences of abs/absf/fabs/fabsf without a "std::" prefix.
79 abs_without_prefix = r"%s(\babsf?\()" % no_std_prefix
80 fabs_without_prefix = r"%s(\bfabsf?\()" % no_std_prefix
81 # Skips matching any lines that have "// NOLINT".
82 no_nolint = r"(?![^\n]*//\s+NOLINT)"
84 expression = re.compile("(%s|%s)%s" %
85 (abs_without_prefix, fabs_without_prefix, no_nolint))
86 if expression.search(contents):
87 missing_std_prefix_files.append(f.LocalPath())
89 result = []
90 if using_std_abs_files:
91 result.append(output_api.PresubmitError(
92 'These files have "using std::abs" which is not permitted.',
93 items=using_std_abs_files))
94 if found_fabs_files:
95 result.append(output_api.PresubmitError(
96 'std::abs() should be used instead of std::fabs() for consistency.',
97 items=found_fabs_files))
98 if missing_std_prefix_files:
99 result.append(output_api.PresubmitError(
100 'These files use abs(), absf(), fabs(), or fabsf() without qualifying '
101 'the std namespace. Please use std::abs() in all places.',
102 items=missing_std_prefix_files))
103 return result
105 def CheckPassByValue(input_api,
106 output_api,
107 white_list=CC_SOURCE_FILES,
108 black_list=None):
109 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
110 source_file_filter = lambda x: input_api.FilterSourceFile(x,
111 white_list,
112 black_list)
114 local_errors = []
116 # Well-defined simple classes containing only <= 4 ints, or <= 2 floats.
117 pass_by_value_types = ['base::Time',
118 'base::TimeTicks',
121 for f in input_api.AffectedSourceFiles(source_file_filter):
122 contents = input_api.ReadFile(f, 'rb')
123 match = re.search(
124 r'\bconst +' + '(?P<type>(%s))&' %
125 string.join(pass_by_value_types, '|'),
126 contents)
127 if match:
128 local_errors.append(output_api.PresubmitError(
129 '%s passes %s by const ref instead of by value.' %
130 (f.LocalPath(), match.group('type'))))
131 return local_errors
133 def CheckTodos(input_api, output_api):
134 errors = []
136 source_file_filter = lambda x: x
137 for f in input_api.AffectedSourceFiles(source_file_filter):
138 contents = input_api.ReadFile(f, 'rb')
139 if ('FIX'+'ME') in contents:
140 errors.append(f.LocalPath())
142 if errors:
143 return [output_api.PresubmitError(
144 'All TODO comments should be of the form TODO(name).',
145 items=errors)]
146 return []
148 def FindUnquotedQuote(contents, pos):
149 match = re.search(r"(?<!\\)(?P<quote>\")", contents[pos:])
150 return -1 if not match else match.start("quote") + pos
152 def FindUselessIfdefs(input_api, output_api):
153 errors = []
154 source_file_filter = lambda x: x
155 for f in input_api.AffectedSourceFiles(source_file_filter):
156 contents = input_api.ReadFile(f, 'rb')
157 if re.search(r'#if\s*0\s', contents):
158 errors.append(f.LocalPath())
159 if errors:
160 return [output_api.PresubmitError(
161 'Don\'t use #if '+'0; just delete the code.',
162 items=errors)]
163 return []
165 def FindNamespaceInBlock(pos, namespace, contents, whitelist=[]):
166 open_brace = -1
167 close_brace = -1
168 quote = -1
169 name = -1
170 brace_count = 1
171 quote_count = 0
172 while pos < len(contents) and brace_count > 0:
173 if open_brace < pos: open_brace = contents.find("{", pos)
174 if close_brace < pos: close_brace = contents.find("}", pos)
175 if quote < pos: quote = FindUnquotedQuote(contents, pos)
176 if name < pos: name = contents.find(("%s::" % namespace), pos)
178 if name < 0:
179 return False # The namespace is not used at all.
180 if open_brace < 0:
181 open_brace = len(contents)
182 if close_brace < 0:
183 close_brace = len(contents)
184 if quote < 0:
185 quote = len(contents)
187 next = min(open_brace, min(close_brace, min(quote, name)))
189 if next == open_brace:
190 brace_count += 1
191 elif next == close_brace:
192 brace_count -= 1
193 elif next == quote:
194 quote_count = 0 if quote_count else 1
195 elif next == name and not quote_count:
196 in_whitelist = False
197 for w in whitelist:
198 if re.match(w, contents[next:]):
199 in_whitelist = True
200 break
201 if not in_whitelist:
202 return True
203 pos = next + 1
204 return False
206 # Checks for the use of cc:: within the cc namespace, which is usually
207 # redundant.
208 def CheckNamespace(input_api, output_api):
209 errors = []
211 source_file_filter = lambda x: x
212 for f in input_api.AffectedSourceFiles(source_file_filter):
213 contents = input_api.ReadFile(f, 'rb')
214 match = re.search(r'namespace\s*cc\s*{', contents)
215 if match:
216 whitelist = [
217 r"cc::remove_if\b",
219 if FindNamespaceInBlock(match.end(), 'cc', contents, whitelist=whitelist):
220 errors.append(f.LocalPath())
222 if errors:
223 return [output_api.PresubmitError(
224 'Do not use cc:: inside of the cc namespace.',
225 items=errors)]
226 return []
228 def CheckForUseOfWrongClock(input_api,
229 output_api,
230 white_list=CC_SOURCE_FILES,
231 black_list=None):
232 """Make sure new lines of code don't use a clock susceptible to skew."""
233 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
234 source_file_filter = lambda x: input_api.FilterSourceFile(x,
235 white_list,
236 black_list)
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:
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*;'
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')')
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()))
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 []
281 def CheckChangeOnUpload(input_api, output_api):
282 results = []
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 += CheckNamespace(input_api, output_api)
289 results += CheckForUseOfWrongClock(input_api, output_api)
290 results += FindUselessIfdefs(input_api, output_api)
291 results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
292 return results
294 def GetPreferredTryMasters(project, change):
295 return {
296 'tryserver.chromium': {
297 'linux_blink_rel': set(['defaulttests']),
299 'tryserver.chromium.gpu': {
300 'linux_gpu': set(['defaulttests']),
301 'mac_gpu': set(['defaulttests']),
302 'win_gpu': set(['defaulttests']),