blob: 4cacf811336530e9cb754534cb3b3c8169d4bac1 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001# Copyright 2012 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6# * Redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer.
8# * Redistributions in binary form must reproduce the above
9# copyright notice, this list of conditions and the following
10# disclaimer in the documentation and/or other materials provided
11# with the distribution.
12# * Neither the name of Google Inc. nor the names of its
13# contributors may be used to endorse or promote products derived
14# from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28"""Top-level presubmit script for V8.
29
30See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
31for more details about the presubmit API built into gcl.
32"""
33
34import sys
35
36
37_EXCLUDED_PATHS = (
38 r"^test[\\\/].*",
39 r"^testing[\\\/].*",
40 r"^third_party[\\\/].*",
41 r"^tools[\\\/].*",
42)
43
44
45# Regular expression that matches code only used for test binaries
46# (best effort).
47_TEST_CODE_EXCLUDED_PATHS = (
48 r'.+-unittest\.cc',
49 # Has a method VisitForTest().
50 r'src[\\\/]compiler[\\\/]ast-graph-builder\.cc',
51 # Test extension.
52 r'src[\\\/]extensions[\\\/]gc-extension\.cc',
53)
54
55
56_TEST_ONLY_WARNING = (
57 'You might be calling functions intended only for testing from\n'
58 'production code. It is OK to ignore this warning if you know what\n'
59 'you are doing, as the heuristics used to detect the situation are\n'
60 'not perfect. The commit queue will not block on this warning.')
61
62
63def _V8PresubmitChecks(input_api, output_api):
64 """Runs the V8 presubmit checks."""
65 import sys
66 sys.path.append(input_api.os_path.join(
67 input_api.PresubmitLocalPath(), 'tools'))
68 from presubmit import CppLintProcessor
69 from presubmit import SourceProcessor
Ben Murdoch62ed6312017-06-06 11:06:27 +010070 from presubmit import StatusFilesProcessor
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071
72 results = []
Ben Murdoch62ed6312017-06-06 11:06:27 +010073 if not CppLintProcessor().RunOnFiles(
74 input_api.AffectedFiles(include_deletes=False)):
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075 results.append(output_api.PresubmitError("C++ lint check failed"))
Ben Murdoch62ed6312017-06-06 11:06:27 +010076 if not SourceProcessor().RunOnFiles(
77 input_api.AffectedFiles(include_deletes=False)):
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 results.append(output_api.PresubmitError(
79 "Copyright header, trailing whitespaces and two empty lines " \
80 "between declarations check failed"))
Ben Murdoch62ed6312017-06-06 11:06:27 +010081 if not StatusFilesProcessor().RunOnFiles(
82 input_api.AffectedFiles(include_deletes=True)):
Ben Murdoch014dc512016-03-22 12:00:34 +000083 results.append(output_api.PresubmitError("Status file check failed"))
Ben Murdoch62ed6312017-06-06 11:06:27 +010084 results.extend(input_api.canned_checks.CheckAuthorizedAuthor(
85 input_api, output_api))
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 return results
87
88
89def _CheckUnwantedDependencies(input_api, output_api):
90 """Runs checkdeps on #include statements added in this
91 change. Breaking - rules is an error, breaking ! rules is a
92 warning.
93 """
94 # We need to wait until we have an input_api object and use this
95 # roundabout construct to import checkdeps because this file is
96 # eval-ed and thus doesn't have __file__.
97 original_sys_path = sys.path
98 try:
99 sys.path = sys.path + [input_api.os_path.join(
100 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
101 import checkdeps
102 from cpp_checker import CppChecker
103 from rules import Rule
104 finally:
105 # Restore sys.path to what it was before.
106 sys.path = original_sys_path
107
108 added_includes = []
109 for f in input_api.AffectedFiles():
110 if not CppChecker.IsCppFile(f.LocalPath()):
111 continue
112
113 changed_lines = [line for line_num, line in f.ChangedContents()]
114 added_includes.append([f.LocalPath(), changed_lines])
115
116 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
117
118 error_descriptions = []
119 warning_descriptions = []
120 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
121 added_includes):
122 description_with_path = '%s\n %s' % (path, rule_description)
123 if rule_type == Rule.DISALLOW:
124 error_descriptions.append(description_with_path)
125 else:
126 warning_descriptions.append(description_with_path)
127
128 results = []
129 if error_descriptions:
130 results.append(output_api.PresubmitError(
131 'You added one or more #includes that violate checkdeps rules.',
132 error_descriptions))
133 if warning_descriptions:
134 results.append(output_api.PresubmitPromptOrNotify(
135 'You added one or more #includes of files that are temporarily\n'
136 'allowed but being removed. Can you avoid introducing the\n'
137 '#include? See relevant DEPS file(s) for details and contacts.',
138 warning_descriptions))
139 return results
140
141
Ben Murdoch014dc512016-03-22 12:00:34 +0000142def _CheckNoInlineHeaderIncludesInNormalHeaders(input_api, output_api):
143 """Attempts to prevent inclusion of inline headers into normal header
144 files. This tries to establish a layering where inline headers can be
145 included by other inline headers or compilation units only."""
146 file_inclusion_pattern = r'(?!.+-inl\.h).+\.h'
147 include_directive_pattern = input_api.re.compile(r'#include ".+-inl.h"')
148 include_warning = (
149 'You might be including an inline header (e.g. foo-inl.h) within a\n'
150 'normal header (e.g. bar.h) file. Can you avoid introducing the\n'
151 '#include? The commit queue will not block on this warning.')
152
153 def FilterFile(affected_file):
154 black_list = (_EXCLUDED_PATHS +
155 input_api.DEFAULT_BLACK_LIST)
156 return input_api.FilterSourceFile(
157 affected_file,
158 white_list=(file_inclusion_pattern, ),
159 black_list=black_list)
160
161 problems = []
162 for f in input_api.AffectedSourceFiles(FilterFile):
163 local_path = f.LocalPath()
164 for line_number, line in f.ChangedContents():
165 if (include_directive_pattern.search(line)):
166 problems.append(
167 '%s:%d\n %s' % (local_path, line_number, line.strip()))
168
169 if problems:
170 return [output_api.PresubmitPromptOrNotify(include_warning, problems)]
171 else:
172 return []
173
174
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
176 """Attempts to prevent use of functions intended only for testing in
177 non-testing code. For now this is just a best-effort implementation
178 that ignores header files and may have some false positives. A
179 better implementation would probably need a proper C++ parser.
180 """
181 # We only scan .cc files, as the declaration of for-testing functions in
182 # header files are hard to distinguish from calls to such functions without a
183 # proper C++ parser.
184 file_inclusion_pattern = r'.+\.cc'
185
186 base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
187 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
188 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
189 exclusion_pattern = input_api.re.compile(
190 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
191 base_function_pattern, base_function_pattern))
192
193 def FilterFile(affected_file):
194 black_list = (_EXCLUDED_PATHS +
195 _TEST_CODE_EXCLUDED_PATHS +
196 input_api.DEFAULT_BLACK_LIST)
197 return input_api.FilterSourceFile(
198 affected_file,
199 white_list=(file_inclusion_pattern, ),
200 black_list=black_list)
201
202 problems = []
203 for f in input_api.AffectedSourceFiles(FilterFile):
204 local_path = f.LocalPath()
205 for line_number, line in f.ChangedContents():
206 if (inclusion_pattern.search(line) and
207 not comment_pattern.search(line) and
208 not exclusion_pattern.search(line)):
209 problems.append(
210 '%s:%d\n %s' % (local_path, line_number, line.strip()))
211
212 if problems:
213 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
214 else:
215 return []
216
217
Ben Murdochf3b273f2017-01-17 12:11:28 +0000218def _CheckMissingFiles(input_api, output_api):
219 """Runs verify_source_deps.py to ensure no files were added that are not in
220 GN.
221 """
222 # We need to wait until we have an input_api object and use this
223 # roundabout construct to import checkdeps because this file is
224 # eval-ed and thus doesn't have __file__.
225 original_sys_path = sys.path
226 try:
227 sys.path = sys.path + [input_api.os_path.join(
228 input_api.PresubmitLocalPath(), 'tools')]
229 from verify_source_deps import missing_gn_files, missing_gyp_files
230 finally:
231 # Restore sys.path to what it was before.
232 sys.path = original_sys_path
233
234 gn_files = missing_gn_files()
235 gyp_files = missing_gyp_files()
236 results = []
237 if gn_files:
238 results.append(output_api.PresubmitError(
239 "You added one or more source files but didn't update the\n"
240 "corresponding BUILD.gn files:\n",
241 gn_files))
242 if gyp_files:
243 results.append(output_api.PresubmitError(
244 "You added one or more source files but didn't update the\n"
245 "corresponding gyp files:\n",
246 gyp_files))
247 return results
248
249
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250def _CommonChecks(input_api, output_api):
251 """Checks common to both upload and commit."""
252 results = []
253 results.extend(input_api.canned_checks.CheckOwners(
254 input_api, output_api, source_file_filter=None))
255 results.extend(input_api.canned_checks.CheckPatchFormatted(
256 input_api, output_api))
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100257 results.extend(input_api.canned_checks.CheckGenderNeutral(
258 input_api, output_api))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 results.extend(_V8PresubmitChecks(input_api, output_api))
260 results.extend(_CheckUnwantedDependencies(input_api, output_api))
261 results.extend(
262 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
Ben Murdoch014dc512016-03-22 12:00:34 +0000263 results.extend(
264 _CheckNoInlineHeaderIncludesInNormalHeaders(input_api, output_api))
Ben Murdochf3b273f2017-01-17 12:11:28 +0000265 results.extend(_CheckMissingFiles(input_api, output_api))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266 return results
267
268
269def _SkipTreeCheck(input_api, output_api):
270 """Check the env var whether we want to skip tree check.
Ben Murdoch014dc512016-03-22 12:00:34 +0000271 Only skip if include/v8-version.h has been updated."""
272 src_version = 'include/v8-version.h'
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000273 if not input_api.AffectedSourceFiles(
274 lambda file: file.LocalPath() == src_version):
275 return False
276 return input_api.environ.get('PRESUBMIT_TREE_CHECK') == 'skip'
277
278
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279def CheckChangeOnUpload(input_api, output_api):
280 results = []
281 results.extend(_CommonChecks(input_api, output_api))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 return results
283
284
285def CheckChangeOnCommit(input_api, output_api):
286 results = []
287 results.extend(_CommonChecks(input_api, output_api))
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288 results.extend(input_api.canned_checks.CheckChangeHasDescription(
289 input_api, output_api))
290 if not _SkipTreeCheck(input_api, output_api):
291 results.extend(input_api.canned_checks.CheckTreeIsOpen(
292 input_api, output_api,
293 json_url='http://v8-status.appspot.com/current?format=json'))
294 return results